package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.dto.ReservationDto;
import com.zzyl.nursing.domain.pojo.NursingProject;
import com.zzyl.nursing.domain.pojo.Reservation;
import com.zzyl.nursing.domain.pojo.TimeSlotEnum;
import com.zzyl.nursing.domain.vo.ReservationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 预约信息Service业务层处理
 * 
 * @author alexis
 * @date 2025-05-27
 */
@Slf4j
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;


    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return this.list(Wrappers.<Reservation>lambdaQuery()
                .eq(ObjectUtils.isNotNull(reservation.getStatus()),Reservation::getStatus,reservation.getStatus()));
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }

    /**
     * 查询当天取消预约数量
     * @return 取消预约数量
     * @author yc
     * @date 2025-05-27
     */
    @Override
    public int getCancelCount() {
        // 获取当天的开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        // 获取当前用户的id
        log.info("MemberRoomTypeService当前线程==>{}",Thread.currentThread()
                .getId());
        Long id = UserThreadLocal.getValue();
        log.info("从ThreadLocal中获取的用户id为:{}",id);

        // 查询当天取消预约数量
        return reservationMapper.getCount(id,startTime,endTime);
    }

    /**
     * 查询时间段的预约次数
     * @return
     */
    @Override
    public List<ReservationVo> getRemainingCounts(Long time) {
//        // 将时间戳转换为 LocalDateTime 对象
//        LocalDateTime localDateTime = Instant.ofEpochMilli(time)
//                .atZone(ZoneId.systemDefault())
//                .toLocalDateTime();
//        // 将时间戳转换为 LocalDateTime 对象
//        LocalDateTime localDateTime1 = LocalDateTimeUtil.of(time);
//
//
//        LocalDateTime newLocalDateTime = localDateTime1.plusMinutes(30);
//        List<Reservation> list = this.list(Wrappers.<Reservation>lambdaQuery().between(Reservation::getTime, localDateTime, newLocalDateTime)
//                .eq(Reservation::getStatus, 0));
//        int count = list.size();
////        int count = 6-size;
//        // 将LocalDateTime对象转换为字符串表示形式
//        // 使用预定义格式 ISO_LOCAL_DATE_TIME
//        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//        String timeStr = localDateTime1.format(formatter);
//
//
////        String timeStr = TimeSlotEnum.getTimeStage(localDateTime);
//        ReservationVo reservationVo = new ReservationVo(timeStr,count);
//        List<ReservationVo> list1 = new ArrayList<>();
//        list1.add(reservationVo);
//        return list1;

        return reservationMapper.getRemaining(time);
    }


    /**
     * 每小时 16 分和 46 分检查预约项目时间是否过期
     */
//    @Scheduled(cron = "0 16,46 * * * ?")
    @Scheduled(cron = "1/15 * * * * ?")
    public void checkReservationExpiration() {

    }

    @Override
    public int insertReserve(ReservationDto reservationDto) {
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservation.setStatus(0);
        Long id = UserThreadLocal.getValue();
        reservation.setUpdateBy(id.toString());
        return this.save(reservation) ? 1:0;
    }

    @Override
    public void cancelReservation(Long id) {
        this.update(Wrappers.<Reservation>lambdaUpdate().eq(Reservation::getId, id).set(Reservation::getStatus, 2).set(Reservation::getUpdateTime, LocalDateTime.now()));
    }

}